from flask import Flask, request, send_file
import tempfile
import subprocess

app = Flask(__name__)

@app.route('/convert', methods=['POST'])
def convert():
    if 'file' not in request.files:
        return {"error": "No file uploaded"}, 400
    
    file = request.files['file']
    with tempfile.NamedTemporaryFile(suffix=".pdf") as tmp:
        file.save(tmp.name)
        # 执行olmOCR转换
        subprocess.run([
            "python", "-m", "olmocr.pipeline", 
            "./", 
            "--markdown", 
            "--pdfs", tmp.name
        ], check=True)
        # 返回转换结果
        return send_file(f"./markdown/{Path(tmp.name).stem}.md")
